// import { Store } from 'tauri-plugin-store-api';

// 模拟 Store 实现
class MockStore {
  private data: Record<string, any> = {};
  private storeName: string;
  
  constructor(storeName: string) {
    this.storeName = storeName;
    console.log(`创建模拟 Store: ${storeName}`);
    
    // 尝试从 localStorage 加载数据 (仅在浏览器环境中)
    try {
      const savedData = localStorage.getItem(`mock-store-${storeName}`);
      if (savedData) {
        this.data = JSON.parse(savedData);
      }
    } catch (e) {
      console.log('使用空数据初始化模拟 Store');
    }
  }
  
  async get(key: string): Promise<any> {
    return this.data[key];
  }
  
  async set(key: string, value: any): Promise<void> {
    this.data[key] = value;
  }
  
  async save(): Promise<void> {
    // 尝试保存到 localStorage (仅在浏览器环境中)
    try {
      localStorage.setItem(`mock-store-${this.storeName}`, JSON.stringify(this.data));
    } catch (e) {
      console.log('模拟存储保存到内存中');
    }
  }
  
  async delete(key: string): Promise<void> {
    delete this.data[key];
  }
  
  async clear(): Promise<void> {
    this.data = {};
  }
  
  async entries(): Promise<[string, any][]> {
    return Object.entries(this.data);
  }
}

// 使用模拟 Store
const Store = MockStore;

// 创建Store实例
const store = new Store('novel-writer-settings.json');

interface Settings {
  openai_api_key?: string;
  deepseek_api_key?: string;
  siliconflow_api_key?: string;
  gemini_api_key?: string;
  anthropic_api_key?: string;
  openai_model?: string;
  selected_model?: string;
  selected_provider?: 'deepseek' | 'siliconflow';
  theme?: 'light' | 'dark' | 'system';
  auto_save?: boolean;
  auto_save_interval?: number;
  editor_font_size?: number;
  editor_font_family?: string;
  editor_line_height?: number;
}

/**
 * 设置管理器
 */
export const settings = {
  /**
   * 获取设置项
   * @param key 设置键
   * @returns 设置值
   */
  async get<K extends keyof Settings>(key: K): Promise<Settings[K] | undefined> {
    try {
      return await store.get(key) as Settings[K];
    } catch (error) {
      console.error(`读取设置${key}失败:`, error);
      return undefined;
    }
  },

  /**
   * 设置配置项
   * @param key 设置键
   * @param value 设置值
   * @returns 成功与否
   */
  async set<K extends keyof Settings>(key: K, value: Settings[K]): Promise<boolean> {
    try {
      await store.set(key, value);
      await store.save();
      return true;
    } catch (error) {
      console.error(`保存设置${key}失败:`, error);
      return false;
    }
  },

  /**
   * 删除设置项
   * @param key 设置键
   * @returns 成功与否
   */
  async delete<K extends keyof Settings>(key: K): Promise<boolean> {
    try {
      await store.delete(key);
      await store.save();
      return true;
    } catch (error) {
      console.error(`删除设置${key}失败:`, error);
      return false;
    }
  },

  /**
   * 获取所有设置
   * @returns 所有设置
   */
  async getAll(): Promise<Partial<Settings>> {
    try {
      const allEntries = await store.entries();
      const settings: Partial<Settings> = {};
      
      for (const [key, value] of allEntries) {
        settings[key as keyof Settings] = value as any;
      }
      
      return settings;
    } catch (error) {
      console.error('获取所有设置失败:', error);
      return {};
    }
  },

  /**
   * 清空所有设置
   * @returns 成功与否
   */
  async clear(): Promise<boolean> {
    try {
      await store.clear();
      await store.save();
      return true;
    } catch (error) {
      console.error('清空设置失败:', error);
      return false;
    }
  }
}; 